package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils

class TimesOfIncomeDistribution_30201_10_filter {

    static List<TaskElementVO.DrawResultVO> doConvert(DependParamsVO dependParams) {

        def Regex_1 = /.收益分配方案.*/
        def Regex_2 = /.*收益分配*/
        def Regex_3 = /.*本计划的收益分配*/
        def Regex = "计划收益分配方案由"

        def textPathMap = dependParams.textPathMap
        List<TaskElementVO.DrawResultVO> trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        for (int i = 0; i < resultVOs.size(); i++) {
            if (StringUtils.isEmpty(resultVOs.get(i).childTaskSn)) {
//                return  resultVOs
                continue
            }
            def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
            def rawText = FileUtils.loadJson(rawPath)
            def drawResultVO = resultVOs[i]
            def expandOffset = 0
            def start = resultVOs.get(i).start
            def end = drawResultVO.end
            def expandA = 0
            while (expandA < 200 && start > expandA) {
                def expandStr = rawText.substring(start - expandA, start)
                if (expandStr =~ Regex_3) {
                    def expand = 0
                    while (expand < 250 && rawText.length() > end + expand) {
                        def expandString = rawText.substring(end, end + expand)
                        def REGEX_STOP = /.*邮件.*。.*/
                        def REGEX_STOP_2 = /.*邮箱.*。.*/
                        if (expandString =~ REGEX_STOP || expandString =~ REGEX_STOP_2) {
                            def JSONTextMap = dependParams.JSONTextMap
                            def fileTextStr = ""
                            fileTextStr = JSONTextMap.get(resultVOs.get(i).childTaskSn)
                            TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                            def startOffset = -expandA
                            def expandStrAll = rawText.substring(start - expandA, end + expand)
                            def index = expandStrAll.indexOf(Regex)
                            if(index>-1){
                                startOffset += index
                            }
                            objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, expand, fileTextStr, rawText)
                            objectNew.text = rawText.substring(start+startOffset, end + expand)
                            objectNew.businessValue = rawText.substring(start+startOffset, end + expand)
                            trueValueSet.add(objectNew)
                            return  doConvert1(trueValueSet,dependParams)
                        }
                        expand++
                    }
                }
                expandA++
            }

            while (expandOffset < 200 && start > expandOffset) {
                def expandStr = rawText.substring(start - expandOffset, start)

                if (expandStr =~ Regex_1) {


                    def expand = 0
                    while (expand < 250 && rawText.length() > end + expand) {
                        def expandString = rawText.substring(start, end + expand)
                        def REGEX_STOP = /.*邮件.*。.*/
                        def REGEX_STOP_2 = /.*邮箱.*。.*/
                        if (expandString =~ REGEX_STOP || expandString =~ REGEX_STOP_2) {
                            def JSONTextMap = dependParams.JSONTextMap
                            def tmpIndex=expandString.indexOf("收益分配方案由")
                            def fileTextStr = ""
                            fileTextStr = JSONTextMap.get(resultVOs.get(i).childTaskSn)
                            TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                            objectNew = TextUtils.correctPositionInfoByOffset(objectNew, 0, expand, fileTextStr, rawText)
                            objectNew.text = rawText.substring(start+tmpIndex, end + expand)
                            objectNew.businessValue = rawText.substring(start+tmpIndex, end + expand)

                            trueValueSet.add(objectNew)

                            return  doConvert1(trueValueSet,dependParams)
                        }
                        expand++
                    }
                }
                expandOffset++
            }
        }

        for (int i = 0; i < resultVOs.size(); i++) {
            if (StringUtils.isEmpty(resultVOs.get(i).childTaskSn)) {
                continue
            }
            def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
            def rawText = FileUtils.loadJson(rawPath)
            def drawResultVO = resultVOs[i]
            def expandOffset = 0
            def start = resultVOs.get(i).start
            def end = drawResultVO.end
            while (expandOffset < 200 && start > expandOffset) {
                def expandStr = rawText.substring(start - expandOffset, start)
                if (expandStr =~ Regex_2) {
                    def expand = 0
                    while (expand < 250 && rawText.length() > end + expand) {
                        def expandString = rawText.substring(start, end + expand)
                        def REGEX_STOP = /.*邮件.*。.*/
                        def REGEX_STOP_2 = /.*邮箱.*。.*/
                        if (expandString =~ REGEX_STOP || expandString =~ REGEX_STOP_2) {
                            def startOffset = 0
                            if( expandString.indexOf("本计划不定期") > 1 ){
                                startOffset = expandString.indexOf("本计划不定期")
                            }
                            def JSONTextMap = dependParams.JSONTextMap
                            def fileTextStr = ""
                            fileTextStr = JSONTextMap.get(resultVOs.get(i).childTaskSn)
                            TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                            objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, expand, fileTextStr, rawText)
                            objectNew.text = rawText.substring(start + startOffset, end + expand)
                            objectNew.businessValue = rawText.substring(start + startOffset, end + expand)
                            trueValueSet.add(objectNew)
                            return doConvert1(trueValueSet,dependParams)
                        }
                        expand++
                    }
                }
                expandOffset++
            }
        }


        if (!CollectionUtils.isEmpty(resultVOs) && resultVOs.size() > 2) {
            trueValueSet.add(resultVOs[0])
            trueValueSet.add(resultVOs[1])
            return trueValueSet
        }


        return resultVOs
    }

    static List<TaskElementVO.DrawResultVO> doConvert1(List<TaskElementVO.DrawResultVO> resultVOs, DependParamsVO dependParams) {
        def textPathMap = dependParams.textPathMap

        List<TaskElementVO.DrawResultVO> trueValueSet = new ArrayList<>()
        for (int i = 0; i < resultVOs.size(); i++) {
            if (StringUtils.isEmpty(resultVOs.get(i).childTaskSn)) {
                continue
            }
            def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
            def rawText = FileUtils.loadJson(rawPath)
            def drawResultVO = resultVOs[i]
            def start = resultVOs.get(i).start
            def end = drawResultVO.end
            def expand = 0
            while (expand < 250 && rawText.length() > end + expand) {
                def expandString = rawText.substring(start, end + expand)
                def REGEX_STOP = /.*附件.*。.*/
                if (expandString =~ REGEX_STOP) {
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(resultVOs.get(i).childTaskSn)
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, 0, expand, fileTextStr, rawText)
                    objectNew.text = rawText.substring(start, end + expand)
                    objectNew.businessValue = rawText.substring(start, end + expand)
                    trueValueSet.add(objectNew)
                    return trueValueSet
                }
                expand++
            }
        }


        return resultVOs
    }

}
